//
//  SecondViewController.m
//  homework_UITableView
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "MyLabel.h"
#import "MyTextField.h"

@interface SecondViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) MyLabel *label;
@property (nonatomic, retain) MyTextField *textField;

@property (nonatomic, retain) UITextField *nameTextField;
@property (nonatomic, retain) UITextField *numTextField;
@property (nonatomic, retain) UITextField *QQTextField;

@end

@implementation SecondViewController

-(void)dealloc
{
    [_indexPath release];
    [_nameTextField release];
    [_numTextField release];
    [_QQTextField release];
    [_label release];
    [_textField release];
    [_image release];
    [_imageView release];
    [super dealloc];
}

-(instancetype)init
{
    self = [super init];
    
    if (self) {
        self.image = [[UIImage alloc] init];
        
        self.indexPath = [[NSIndexPath alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = self.text;
    
    [self createSubViews];
    
    [self createText];
}

-(void)createSubViews
{
    CGFloat imageViewW = (CGRectGetWidth(self.view.bounds) - 40) / 2;
    CGFloat imageViewH = CGRectGetHeight(self.view.bounds) / 3;
    CGFloat imageViewX = (CGRectGetWidth(self.view.bounds) - imageViewW) / 2;
    CGFloat imageViewY = 20;
    
    self.imageView = [[UIImageView alloc]initWithImage:self.image];
   
    self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
    [self.view addSubview:self.imageView];
    
    [self.imageView release];
}

-(void)createText
{
    CGFloat height = CGRectGetHeight(self.view.frame) / 3 + 20;
    CGFloat width = CGRectGetWidth(self.view.frame);
    NSArray *array = @[@"姓名:", @"电话号码:", @"QQ号:"];
    
    for (int i = 0; i < 3; i++) {
        self.label = [[MyLabel alloc]initWithFrame:CGRectMake(40, height + 40 * i, width / 4, 50) WithText:array[i]];
        
        [self.view addSubview:self.label];
        [self.label release];
    }

//    NSArray *newArray = @[self.nameText, self.numText, self.QQText];
//    for (int i = 0; i < 3; i++) {
//        self.textField = [[MyTextField alloc]initWithFrame:CGRectMake(width / 3 * 2 - 80, height + 40 * i, width / 4 + 20, 50) withText:newArray[i]];
//        self.textField.delegate = self;
//        [self.view addSubview:self.textField];
//        [self.textField release];
//    }
    
    /**nameTextField*/
    self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(width / 3 * 2 - 80, height, width / 4, 50)];
    self.nameTextField.delegate = self;
    self.nameTextField.text = self.nameText;
    self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.nameTextField];
    [self.nameTextField release];
    
    /**numTextField*/
    self.numTextField = [[UITextField alloc]initWithFrame:CGRectMake(width / 3 * 2 - 80, height + 40, width / 4 + 30, 50)];
    self.numTextField.delegate = self;
    self.numTextField.text = self.numText;
    self.numTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.numTextField];
    [self.numTextField release];
    
    /**QQTextField*/
    self.QQTextField = [[UITextField alloc]initWithFrame:CGRectMake(width / 3 * 2 - 80, height + 40 * 2, width / 4 + 30, 50)];
    self.QQTextField.delegate = self;
    self.QQTextField.text = self.QQText;
    self.QQTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.QQTextField];
    [self.QQTextField release];
}

/**键盘回收*/
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - 视图将要消失
-(void)viewWillDisappear:(BOOL)animated
{
    [self.delegate sendMessageWithName:self.nameTextField.text withNum:self.numTextField.text withQQ:self.QQTextField.text withImage:self.image withIndexPath:self.indexPath];
        
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

@end













